package com.vpx.server.mapper.system;

import com.vpx.server.bean.entity.SysDicType;
import com.vpx.server.bean.info.system.SysDicTypeInfo;
import com.vpx.server.bean.order.system.SysDicTypePageOrder;
import com.vpx.server.framework.base.BaseMapper;
import com.vpx.server.framework.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 数据字典类型
 * @description: 数据字典类型MAPPER
 * @date 2021/09/20 17:35
 */
public interface SysDicTypeMapper extends BaseMapper<SysDicType> {

    /**
     * 修改数据字典类型状态
     * @param id
     * @param status
     * @return
     */
    @Update("update sys_dic_type set status = #{status} where id = #{id}")
    int updateDicTypeStatusById(@Param("id") Long id, @Param("status") String status);

    /**
     * 根据id查询数据字典类型
     * @param id
     * @return
     */
    @Select("select * from sys_dic_type where status = 'ABLE' and id = #{id}")
    SysDicType querySysDicTypeById(@Param("id") Long id);

    /**
     * 根据编码查询数据字典类型
     * @param code
     * @return
     */
    @Select("select * from sys_dic_type where status = 'ABLE' and code = #{code}")
    SysDicType queryDicTypeByCode(@Param("code") String code);

    /**
     * 根据编码查询记录条数
     * @param code
     * @return
     */
    @Select("select count(0) from sys_dic_type where code = #{code} and status = 'ABLE'")
    int querySysDicTypeByCode(@Param("code") String code);

    /**
     * 根据ID和编码查询记录条数
     * @param id
     * @param code
     * @return
     */
    @Select("select count(0) from sys_dic_type where id = #{id} and code = #{code} and status = 'ABLE'")
    int querySysDicTypeByIdAndCode(@Param("id") Long id, @Param("code") String code);

    /**
     * 分页查询数据字典类型
     * @param order
     * @return
     */
    @SelectProvider(type = SqlProvider.class, method = "pageQuery")
    List<SysDicTypeInfo> pageQuery(@Param("order") SysDicTypePageOrder order);

    class SqlProvider{
        public String pageQuery(@Param("order") SysDicTypePageOrder order){
            StringBuffer sb = new StringBuffer("select * from sys_dic_type where status = 'ABLE'");
            if (StringUtils.isNotBlank(order.getCode())) {
                sb.append(" and code like ('%'+#{order.code}+'%')");
            }
            if (StringUtils.isNotBlank(order.getName())) {
                sb.append(" and name like ('%'+#{order.name}+'%')");
            }
            sb.append(" order by create_time desc");
            return sb.toString();
        }
    }
}
